﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WCS.Utils
{
    public class LogHelper
    {
        public delegate void Logdelegate(string tableName, string logType, string logInfo, string logLevel);
        //声明对象
        public static Logdelegate InvokeLogIntegrationWithOutDB = WriteLogIntegrationWithOutDB;
        //public static Logdelegate InvokeLogIntegration = WriteLogIntegration;


        public static void WriteLogIntegrationWithOutDB(string tableName, string logType, string logInfo, string logLevel)
        {
            try
            {
                string msg = string.Concat(logType, "【", logLevel, "】：", logInfo);

                if ("error".Equals(logLevel.ToLower()))
                    WriteError(msg);
                else
                    WriteLog(msg);

                //InsertAndGetString(msg);
            }
            catch (Exception) { }
        }

        public static void WriteLogIntegration(string tableName, string logType, string logInfo, string logLevel)
        {
            try
            {
                string msg = string.Concat(logType, "【", logLevel, "】：", logInfo);

                if ("error".Equals(logLevel.ToLower()))
                    WriteError(msg);
                else
                    WriteLog(msg);

                //InsertAndGetString(msg);

                //WriteLogToDataBase(tableName, logType, logInfo, logLevel, ref SystemMemoryClass.iErrorCode, ref SystemMemoryClass.ErrorText);
            }
            catch (Exception) { }
        }

        /// <summary>
        /// 按每天记录日志到启动目录下“日志记录”文件夹内，日志文件命名为《日志文件yyyy-MM-dd.txt》，
        /// 日志文件记录时间可能存与数据库日志时间不一致，因此需调整服务器时间与工作站时间保持一致。
        /// </summary>
        /// <param name="sMsg">日志信息</param>
        public static void WriteLog(string sMsg)
        {
            if (sMsg != "")
            {
                string filename = "日志信息" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                try
                {
                    string sPath = Application.StartupPath + "\\日志记录";
                    string location = sPath + "\\" + filename;
                    if (!Directory.Exists(sPath))//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(sPath);//创建该文件夹
                    }
                    FileInfo fi = new FileInfo(sPath + "\\" + filename);
                    if (!fi.Exists)
                    {
                        using (StreamWriter sw = fi.CreateText())
                        {
                            //sw.WriteLine(DateTime.Now + "\n\t" + sMsg + "\n");
                            sw.WriteLine(DateTime.Now + "  " + sMsg + "\n");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = fi.AppendText())
                        {
                            sw.WriteLine(DateTime.Now + "  " + sMsg + "\n");
                            sw.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// 按每天记录错误信息到启动目录下“错误记录”文件夹内，错误记录文件命名为《错误信息yyyy-MM-dd.txt》，
        /// 错误日志文件记录时间可能存与数据库日志时间不一致，因此需调整服务器时间与工作站时间保持一致。
        /// </summary>
        /// <param name="sMsg">日志信息</param>
        public static void WriteError(string sMsg)
        {
            if (sMsg != "")
            {
                string filename = "错误信息" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                try
                {
                    string sPath = Application.StartupPath + "\\错误记录";
                    string location = sPath + "\\" + filename;
                    if (!Directory.Exists(sPath))//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(sPath);//创建该文件夹
                    }
                    FileInfo fi = new FileInfo(sPath + "\\" + filename);
                    if (!fi.Exists)
                    {
                        using (StreamWriter sw = fi.CreateText())
                        {
                            sw.WriteLine(DateTime.Now + "\n\t" + sMsg + "\n");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = fi.AppendText())
                        {
                            sw.WriteLine(DateTime.Now + "\n\t" + sMsg + "\n");
                            sw.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        //public static void WriteWmsError(string message)
        //{
        //    //创建日志记录组件实例
        //    ILog logger = LogManager.GetLogger("WMS-Error");

        //    if (logger.IsErrorEnabled)
        //    {
        //        logger.Error(message);
        //    }
        //}

        //public static void WriteWmsInfo(string message)
        //{
        //    //创建日志记录组件实例
        //    ILog logger = LogManager.GetLogger("WMS-Info");

        //    if (logger.IsInfoEnabled)
        //    {
        //        logger.Info(message);
        //    }
        //}

        //public static void WriteAgvError(string message)
        //{
        //    //创建日志记录组件实例
        //    ILog logger = LogManager.GetLogger("AGV-Error");

        //    if (logger.IsErrorEnabled)
        //    {
        //        logger.Error(message);
        //    }
        //}

        //public static void WriteAgvInfo(string message)
        //{
        //    //创建日志记录组件实例
        //    ILog logger = LogManager.GetLogger("AGV-Info");

        //    if (logger.IsInfoEnabled)
        //    {
        //        logger.Info(message);
        //    }
        //}

        //public static void WritePtlError(string message)
        //{
        //    //创建日志记录组件实例
        //    ILog logger = LogManager.GetLogger("PTL-Error");

        //    if (logger.IsErrorEnabled)
        //    {
        //        logger.Error(message);
        //    }
        //}

        //public static void WritePtlInfo(string message)
        //{
        //    //创建日志记录组件实例
        //    ILog logger = LogManager.GetLogger("PTL-Info");

        //    if (logger.IsInfoEnabled)
        //    {
        //        logger.Info(message);
        //    }
        //}
    }
}
